package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.util.DigestUtils;


import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {

        return userMapper.findAll();
    }



    @Override
    public String login(User user) {
        String password = user.getPassword();
       String md5Pass=DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass);
        User userDB=userMapper.findUserByUP(user);
        if(userDB==null){
            return null;
        }
    String token = UUID.randomUUID().toString().replace("_", "");

        return token;
    }



    //    @Override
//    public PageResult findList(PageResult pageResult) {
//        //1.获取记录总数
//        long total= userMapper.findCount();
//        //2.获取分页结果
//        int size=pageResult.getPageSize();//条数
//        int start=(pageResult.getPageNum()-1)*size;
//        String query=pageResult.getQuery();//查询条件
//        List<User> list= userMapper.findUserLimit(query,start,size);
//        pageResult.setTotal(total).setRows(list);
//        return pageResult;
//    }
@Override
public PageResult findList(PageResult pageResult) {
        //1.获取记录总和
    long total=userMapper.findCount();
    //2.获取分页查询
    int size =pageResult.getPageSize();//条数
    int start=(pageResult.getPageNum()-1)*size;
    String query=pageResult.getQuery();//查询条件
     List<User> list=  userMapper.findUserLimit(query,start,size);
    pageResult.setTotal(total).setRows(list);
    return pageResult;
}
    @Override
    public void updateStatus(User user) {
        user.setUpdated(new Date());//时间
        userMapper.updateStatus(user);
    }

    @Override
    public void addUser(User user) {
        Date data=new Date();
        String password=user.getPassword();
        String md5Digest= DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Digest).setStatus(true).setCreated(data).setUpdated(data);
        userMapper.addUser(user);
    }

    @Override
    public void deleteUser(User user) {
        userMapper.deleteUser(user);
    }

    @Override
    public User findUserById(Integer id) {
       User user= userMapper.findUserById(id);
        return user;
    }

    @Override
    public void updateUser(User user) {
        user.setUpdated(new Date());
        userMapper.updateUser(user);

    }


}
